using System;
using System.Globalization;
using System.IO;
using EnvDTE;
using VSSDKAssist.Assets.Helpers;
using VSSDKAssist.Assets.ValueProviders.Base;

namespace VSSDKAssist.Recipes.Common.ValueProviders
{
    /// <summary>
    /// Value provider for the VSPackage resx ProjectItem
    /// </summary>
    public class VSPackageResxProjectItemValueProvider : ValueProviderBase
    {
        #region Constants
        private const string VSPackageCodeClassArgument = "VSPackageCodeClassArgument";
        #endregion

        #region Fields
        private string vsPackageCodeClassArgument;
        #endregion

        /// <summary>
        /// Contains code that will be called when recipe execution begins. This is the first method in the lifecycle.
        /// </summary>
        /// <param name="currentValue">An <see cref="T:System.Object"/> that contains the current value of the argument.</param>
        /// <param name="newValue">When this method returns, contains an <see cref="T:System.Object"/> that contains
        /// the new value of the argument, if the returned value
        /// of the method is <see langword="true"/>. Otherwise, it is ignored.</param>
        /// <returns>
        /// 	<see langword="true"/> if the argument value should be replaced with
        /// the value in <paramref name="newValue"/>; otherwise, <see langword="false"/>.
        /// </returns>
        /// <remarks>By default, always returns <see langword="false"/>, unless overriden by a derived class.</remarks>
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            newValue = null;

            if((currentValue == null) && (this.ExecutionStage != Stage.OnBeforeActions))
            {
                CodeClass vsPackageCodeClass = DictionaryService.GetValue(vsPackageCodeClassArgument) as CodeClass;

                if(vsPackageCodeClass != null)
                {
                    string resxFileName =
                        string.Format(
                            CultureInfo.CurrentCulture,
                            "{0}.{1}",
                            Path.GetFileNameWithoutExtension(vsPackageCodeClass.ProjectItem.Name),
                            "resx");

                    ProjectItem item = VSIPHelper.FindItemByName(vsPackageCodeClass.ProjectItem.ContainingProject.ProjectItems, resxFileName, true);

                    if(item != null)
                    {
                        newValue = item;
                        return true;
                    }
                    else
                    {
                        //HAAK specific for VS 2008 SDK 1.0 
                        item = VSIPHelper.FindItemByName(vsPackageCodeClass.ProjectItem.ContainingProject.ProjectItems, "VSPackage.resx", true);

                        if(item != null)
                        {
                            newValue = item;
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Called when [configure].
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        protected override void OnConfigure(System.Collections.Specialized.StringDictionary attributes)
        {
            if(attributes == null)
                throw new ArgumentNullException("attributes");

            if(attributes.ContainsKey(VSPackageCodeClassArgument))
            {
                vsPackageCodeClassArgument = attributes[VSPackageCodeClassArgument];
            }
        }
    }
}